package com.uroad.liaoningpos.database;

import android.content.Context;

import com.uroad.lib.util.log.LogUtil;

import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * 对数据库的表进行CRUD
 * Created by jiadong on 2016/11/7.
 */

public class DaoUtils {

    private DbUtils manager;
    private String  DATABASE_NAME = "liaoningpos.db";

    public DaoUtils(Context context) {
        manager = DbUtils.getInstance();
        manager.init(context, DATABASE_NAME);
    }

    public DbUtils getManager(){
        return  manager;
    }

    /**
     * 完成对数据库中表的插入
     *
     * @param t
     * @return
     */
    public <T> boolean insert(T t) {
        boolean flag = false;
        try {
            flag = manager.getDaoSession().insert(t) != -1 ? true : false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 插入多条记录，需要开启新的线程
     *
     * @param list
     * @return
     */
    public <T> boolean insertList(final List<T> list) {
        boolean flag = false;
        try {
            manager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (T t : list) {
                        manager.getDaoSession().insertOrReplace(t);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 完成某一条记录的修改
     *
     * @param t
     * @return
     */
    public <T> boolean update(T t) {
        boolean flag = false;
        try {
            manager.getDaoSession().update(t);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条数据(按照指定的id进行删除 delete from user where _id = ?)
     *
     * @param t
     * @return
     */
    public <T> boolean delete(T t) {
        boolean flag = false;
        try {
            manager.getDaoSession().delete(t);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除表的所有数据
     *
     * @param cls
     */
    public boolean deleteAll(Class cls) {
        boolean flag = false;
        try {
            manager.getDaoSession().deleteAll(cls);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 返回表的所有数据
     *
     * @param cls
     * @return
     */
    public <T> List<T> getAll(Class cls) {
        List<T> list = null;
        try {
            return manager.getDaoSession().loadAll(cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 按照主键返回单行数据
     *
     * @param cls
     * @param id
     * @return
     */
    public <T> T getOne(Class cls, long id) {
        T t = null;
        try {
            t = (T) manager.getDaoSession().load(cls, id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 使用native sql进行查询
     *
     * @param cls
     * @param sql
     * @param selectionArgs
     * @return
     */
    public <T> List<T> queryBySQL(Class cls, String sql, String[] selectionArgs) {
        List<T> list = null;
        try {
            list = manager.getDaoSession().queryRaw(cls, sql, selectionArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 使用查询构建器进行查询
     * <p>
     * * select  * from user where name like ? or age =? ...
     * >  <  <=  >=  !=  in  between  and  limit ...
     *
     * @param builder
     * @param <T>
     * @return
     */
    public <T> List<T> queryByBuilder(QueryBuilder<T> builder) {
        List<T> list = null;
        try {
            list = builder.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 关闭所有的操作,数据库开启的时候，使用完毕了必须要关闭
     */
    public void closeConnection() {
        if(manager!=null){
            manager.closeConnection();
        }
    }

    public boolean execlSQL(String sql,Object[] args){
        boolean flag = true;
        try {
            Database db = manager.getDaoMaster().getDatabase();
            if(args != null && args.length > 0){
                db.execSQL(sql,args);
            }else{
                db.execSQL(sql);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.e(getClass().getSimpleName(),"execlSQL-->执行SQL语句异常："+e.toString());
            return false;
        }
        return flag;

    }
}
